var Department = (function() {

	function Department() {
		
	}

	function getModuleName() {
		return "部门";
	}
	
	Department.prototype.Messager_Title = function(){
		return '温馨提示';
	}
	
	Department.prototype.Data_Id = function (row){
		return {'departmentId':row['departmentId']};
	}

	Department.prototype.init = function() {
		$department.treegrid
				.treegrid({
					url : arguments[0],
					rownumbers : true,
					method:'get',
					lines : true,
					fit:true,
					fitColumns : true,
					idField : 'departmentId',
					treeField : 'departmentName',
					onSelect : $department.rowClick,
					columns : [ [ {
						field : 'departmentId',
						title : 'ID编号',
						hidden : true,
						width : 100
					}, {
						field : 'departmentName',
						title : '部门名称',
						width : 100
					}, {
						field : 'departmentParent',
						title : '上级部门',
						hidden : true,
						width : 100
					}, {
						field : 'createTimeT',
						title : '创建时间',
						width : 100
					}, {
						field : 'status',
						title : '部门状态',
						formatter : function(val, row) {
							if (val == 'NORMAL') {
								return '正常';
							} else {
								return '<font style="color:red">冻结</font>';
							}
						},
						width : 100
					}, {
						field : 'departmentDescription',
						title : '部门描述',
						width : 100
					} ] ]
				});
		if($department.toolbars.length==0){//使用默认配置
			var $group_buttons = $('.north-menu .button-group button');
			$department.toolbars = [$group_buttons.eq(1),$group_buttons.eq(2),$group_buttons.eq(3),$group_buttons.eq(4),$group_buttons.eq(5)];
		}
		
	}
	
	Department.prototype.rowClick = function() {
		var row = $department.treegrid.treegrid('getSelected');
		if(!row)return;
		if (row.status == 'NORMAL') {
			$department.toolbars[0].attr('disabled',false);
			$department.toolbars[1].attr('disabled',false);
			$department.toolbars[2].attr('disabled',false);
			$department.toolbars[3].attr('disabled',false);
			$department.toolbars[4].attr('disabled',true);
		} else {
			$department.toolbars[1].attr('disabled',true);
			$department.toolbars[2].attr('disabled',true);
			$department.toolbars[3].attr('disabled',true);
			$department.toolbars[4].attr('disabled',false);
		}
	}

	Department.prototype.del = function() {
		var row = $department.treegrid.treegrid('getSelected');
		if (row) {
			$.messager.confirm('删除确认', "您确认要删除这条" + getModuleName() + "么?",
					function(r) {
						if (r) {
							$.messager.progress();
							$.post($department.delUrl, $department.Data_Id(row) , function(data) {
								if (data == "success") {
									msgShow('操作成功', getModuleName() + '删除成功.');
									treeGridReload();
									initButtons();
								} else {
									$.messager.alert('删除错误', data, 'error');
								}
								$.messager.progress('close');
							});
						}
					});
			return;
		}
		$.messager.alert('请选择', "请选择要删除的" + getModuleName() + ".", 'error');
	}

	Department.prototype.add = function() {
		$department.button = true;
		
	}

	Department.prototype.edit = function() {
		$department.button = false;
	}

	Department.prototype.congeal = function() {// 冻结
		$.messager.progress('close');
		$.messager.progress();
		var row = $department.treegrid.treegrid('getSelected');
		$.post($department.congealUrl, $department.Data_Id(row) , function(data) {
			$.messager.progress('close');
			if (data == "success") {
				msgShow('操作成功', getModuleName() + '冻结成功.');
				treeGridReload();
			} else {
				$.messager.alert('冻结错误', data, 'error');
			}
		});
	}

	Department.prototype.thaw = function($self) {// 解冻
		
		if($self.attr('not_move')&&$self.attr('not_move')=='true'){
			var append_obj = $($('.move_right').eq($self.index()-1).html()).appendTo($("#auto_panel"));
			append_obj.html('<strong>操作成功：</strong>'+getModuleName()+'解冻成功.');
			setTimeout(function(){
				append_obj.fadeOut(function(){
					panel_refresh();
				});//2秒后消失
				
			},1200);
		}
	}

	function treeGridReload() {
		$department.treegrid.treegrid('options').url = $department.loadUrl;
		$department.treegrid.treegrid('reload');
		$department.treegrid.treegrid('unselectAll');
		initButtons();
	}

	function addParent() {
		var departmentParent = $(
				'<table data-options="lines:true" name="departmentParent.departmentId" id="departmentParent" style="width:494px;"></table>')
				.appendTo($department.departmentParentBox);
		departmentParent
				.combotree({
					url : arguments.length == 1 ? $department.loadUrl : null,
					valueField : 'departmentId',
					textField : 'departmentName',
					editable : false,
					required : true,
					onBeforeExpand : function(node) {
						departmentParent.combotree('tree').tree('options').url = $department.loadUrl
								+ '&departmentId=' + node.departmentId;
					}
				});
		return departmentParent;
	}

	function initButtons() {
		$('#sudtm-menu-edit').linkbutton('disable');
		$('#sudtm-menu-remove').linkbutton('disable');
		$('#sudtm-menu-congeal').menubutton('disable');
		$('#sudtm-menu-thaw').menubutton('disable');
	}

	function dialogOpen(title,read) {
		$.messager.progress('close');
		var save_btn = {
			text : title,
			iconCls : 'icon-d-save',
			handler : $department.save
		},close_btn = {
			text : '取消',
			iconCls : 'icon-d-close',
			handler : $department.close	
		};
		var buttons = [];
		if(read){
			$.extend(close_btn,{text:'关闭'});//克隆属性
		}else{
			buttons.push(save_btn);
		}
		buttons.push(close_btn);
		$department.dialog.show().dialog({
			title : '&nbsp;'+title+getModuleName(),
			width:600,
			modal:true,
			buttons : buttons
		});
	}
	
	function formSubmit(url,successInfo,title) {
		$department.form.form('submit', {
			url : url,
			onSubmit : function() {
				var isValid = $(this).form('validate');
				if (isValid) {
					$.messager.progress('close');
				}
				if (isValid) {
					$.messager.progress();
				}
				return $(this).form('validate');
			},
			success : function(data) {
				$.messager.progress('close');
				if (data == 'success') {
					msgShow($department.Messager_Title(), getModuleName() + successInfo +'成功.');
					dialogClose($department.success_refresh);
					if($department.success_refresh){//成功后刷新
						treeGridReload();
					}
				} else {
					dialogOpen(successInfo);
					if(dataFilter(data).length>0){
						$.messager.alert($department.Messager_Title(), data, 'error');
					}
				}
			}
		});
	}

	Department.prototype.save = function() {
		if ($department.button) {
			formSubmit($department.addUrl,'添加');// 表单添加
		} else {
			formSubmit($department.editUrl,'编辑');// 表单编辑
		}
	}

	Department.prototype.close = function(url) {
		dialogClose();
	}
	
	function dialogClose() {
		$department.dialog.dialog('close');
	}

	return Department;
})();
